﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcModels.Models;

namespace MvcModels.Controllers
{
    public class HomeController : Controller
    {
        private Person[] personData = {
            new Person {PersonId = 1, FirstName = "Adam", LastName = "Freeman", Role = Role.Admin},
            new Person {PersonId = 2, FirstName = "Steven", LastName = "Sanderson", Role = Role.Admin},
            new Person {PersonId = 3, FirstName = "Jacqui", LastName = "Griffyth", Role = Role.User},
            new Person {PersonId = 4, FirstName = "John", LastName = "Smith", Role = Role.User},
            new Person {PersonId = 5, FirstName = "Anne", LastName = "Jones", Role = Role.Guest}
                                      };
        public ActionResult Index(int id)
        {
            Person dataItem = personData.Where(p => p.PersonId == id).First();
            return View(dataItem);
        }

        public ActionResult CreatePerson()
        {
            return View(new Person());
        }
        [HttpPost]
        public ActionResult CreatePerson(Person model)
        {
            return View("Index", model);
        }

        /*
         * De esta manera cambia el prefijo a aplicar a la clase poniendolo como HomeAdress
        Esto es para el caso de que se haya hecho un submit con Person, ya que ambas comparten los atributos
         * AddressSummary esperaria City y Country, pero hay que agregarle el prefijo HomeAddress (que es como se había mandado)
         * Person.HomeAddress.City
         * 
         * Por otro lado Exclude es para excluir binding de ciertas propiedades
         */
        public ActionResult DisplaySummary([Bind(Prefix = "HomeAddress", Exclude = "Country")]AddressSummary summary)
        {
            return View(summary);
        }

        public ActionResult Names(string[] names)
        {
            names = names ?? new string[0];
            return View(names);
        }

        /*
         * name="[].City"
         * The properties prefixed with [0] are used for the first AddressSumary object
         */
        //public ActionResult Address(IList<AddressSummary> addresses)
        //{
        //    addresses = addresses ?? new List<AddressSummary>();
        //    return View(addresses);
        //}

        public ActionResult Address()
        {
            IList<AddressSummary> addresses = new List<AddressSummary>();
            UpdateModel(addresses);//Manually Invoking Model Binding (lanza excepciones), sino usar TryUpdateModel
            /*Opcionalmente se le puede pasar otro parametro que restringe los valores donde busca*/
            /*
                FormValueProvider
                RouteDataValueProvider
                QueryStringValueProvider
                HttpFileCollectionValueProvider
             */
            return View(addresses);
        }

    }
}
